from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from frp_page_object.base_page import BasePage
from time import sleep


class DeliveryNoticesPage(BasePage):
    loc_delivery_number = (By.XPATH, '//*[contains(text(),"配送单号")]/..//*[@ref="eSortDesc"]/../..')
    loc_edit_button = (By.XPATH, '//div[@class="ag-pinned-right-cols-container"]/div[1]//a')
    loc_address = (By.XPATH, '//*[@id="address"]/../..')

    def desc_delivery_number(self):
        while True:
            try:
                self.locator((By.XPATH, '//*[contains(text(),"配送单号")]/..//*[@ref="eSortDesc"]['
                                        '@class="ag-sort-indicator-icon ag-sort-descending-icon"]'), 3)
            except NoSuchElementException:
                self.click(self.loc_delivery_number, 2)
            else:
                break

    def click_edit(self):
        sleep(5)
        self.click(self.loc_edit_button)

    def click_transport_way(self, transport_way):
        self.click_text_in(transport_way, (By.XPATH, '//*[@id="transportWay"]/../..'), (By.XPATH, '//*[@id="transportWay_list"]'))

    def input_transport_describe(self, transport_describe):
        self.input((By.ID, 'transportDescribe'), transport_describe)

    def input_address(self):
        self.click(self.loc_address)
        self.click((By.XPATH, '//ul[@class="ant-cascader-menu"][1]/li'))
        self.click((By.XPATH, '//ul[@class="ant-cascader-menu"][2]/li'))
        self.click((By.XPATH, '//ul[@class="ant-cascader-menu"][3]/li'))

    def select_output_warehouses(self):
        sleep(2)
        self.click((By.XPATH, '//*[contains(text(),"请选择")]'))
        self.click((By.XPATH, '//div[@class="ant-modal-body"]//div[@class="ag-pinned-left-cols-container"]//input'))
        self.click((By.XPATH, '//*[contains(text(),"确 定")]'))

    def submit(self):
        self.click((By.XPATH, '//div[@class="buttonList"]//*[contains(text(),"保 存")]'))
        self.click((By.XPATH, '//*[contains(text(),"提 交")]'))
        self.click((By.XPATH, '//*[contains(text(),"审 核")]'))
